const { formatResponseBody } = require("./tools");

/**
 * 错误处理基类
 */
class ServerError extends Error {
    constructor(msg, code) {
        super(msg);
        this.code = code;
    }

    // 给客户端响应错误
    toResponseJSON() {
        return formatResponseBody({ code: this.code, msg: this.message });
    }
}
exports.ServerError = ServerError;


/**
 * 文件上传错误
 */
exports.UploadError = class UploadError extends ServerError {
    constructor(msg) {
        super(msg, 413);
    }
}

/**
 * 禁止访问错误
 */
exports.ForbiddenError = class ForbiddenError extends ServerError {
    constructor(msg) {
        super(msg, 401);
    }
}

/**
 * 验证错误
 */
exports.ValidationError = class ValidationError extends ServerError {
    constructor(msg) {
        super(msg, 406);
    }
}

/**
 * 无资源错误
 */
exports.NotFoundError = class NotFoundError extends ServerError {
    constructor(msg) {
        super(msg, 406);
    }
}

/**
 * 未知错误
 */
exports.UnknownError = class UnknownError extends ServerError {
    constructor() {
        super("Server internal error", 500);
    }
}